package com.LibreGeoSocial.App.Routes;


import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.Api.GooglePlaceMark;


public class GoogleRoutesHandler extends DefaultHandler{

     
     private boolean in_name = false;
     private boolean in_description = false;
     private boolean in_coordinates = false;
     private boolean in_lineString = false;
     
     private static String mName = null;
     private static String mDescription = null;
     
     ArrayList<GeoNode> mPlaces = new ArrayList<GeoNode>();
     
     ArrayList<GeoNode> mRoutePath = new ArrayList<GeoNode>();
     
     @Override
     public void startDocument() throws SAXException {
          
     }

     @Override
     public void endDocument() throws SAXException {
          // Nothing to do
     }
     
     public ArrayList<GeoNode> getRoutePath ()
     {
    	 return mRoutePath;
     }
     
     public ArrayList<GeoNode> getPlaces ()
     {    	 
    	 return mPlaces;
     }

     /** Gets be called on opening tags like:
      * <tag>
      * Can provide attribute(s), when xml was like:
      * <tag attribute="attributeValue">*/
     @Override
     public void startElement(String namespaceURI, String localName,
               String qName, Attributes atts) throws SAXException {
          if (localName.equals("name")) {
               this.in_name = true;              
          }
          else if (localName.equals("description")) {
        	  this.in_description = true;      
          }
          else if (localName.equals("coordinates")) {
        	  this.in_coordinates = true;      
          }
          else if (localName.equals("LineString")) {
        	  this.in_lineString = true;      
          }
     }
     
     /** Gets be called on closing tags like:
      * </tag> */
     @Override
     public void endElement(String namespaceURI, String localName, String qName)
               throws SAXException {
          if (localName.equals("name")) {
               this.in_name = false;
          }
          else if (localName.equals("description")) {
        	  this.in_description = false;      
          }
          else if (localName.equals("coordinates")) {
        	  this.in_coordinates = false;      
          }
          else if (localName.equals("LineString")) {
        	  this.in_lineString = false;      
          }
          
          
     }
     
     /** Gets be called on the following structure:
      * <tag>characters</tag> */
     @Override
    public void characters(char ch[], int start, int length) {
    	 
    	 if(this.in_name)
    	 {
    		mName = new String(ch, start, length);
         }
    	 else if (this.in_description)
    	 {
    		 mDescription = new String(ch, start, length);
    		 mDescription = mDescription.replaceFirst("&#160;", " ");
    	 }
    	 else if(this.in_coordinates)
    	 {
    		
    		if (this.in_lineString)
    		{
    			String route = new String(ch, start, length);
    			String[] points = route.split(" ");
    			
    			for (int i=0; i<points.length; i++)
    			{
    				String[] coordenates = points[i].split(",");
    				
    				if (coordenates.length>2)
    				{
    					try
    					{
	    					GooglePlaceMark point = new GooglePlaceMark (null,
	    																	Double.parseDouble(coordenates[1]),
	    																	Double.parseDouble(coordenates[0]),
	    																	null,
	    																	null,
	    																	null);
	    						
	    					mRoutePath.add(point);
    					}
    					catch (Exception e) {

						}
    				}
    			}
    			
    			 Log.e("GoogleRoutesHandler", String.valueOf(mRoutePath.size()));
    		}
    		else if (mName!=null && mDescription!=null)
    		{
    			String coordinates = new String(ch, start, length);
	    		 
	    		String[] coord = coordinates.split(",");
	    		 
	    		if (coord.length >= 2)
	    		{
	    		 
	    			GooglePlaceMark myPlace = new GooglePlaceMark (null,
						    				 					   Double.parseDouble(coord[1]),
						    				 					   Double.parseDouble(coord[0]),
						    				 					   0.0,
						    				 					   null,
						    				 					   null,
						    				 					   mName,
						    				 					   mDescription);
		    		     		 
	    			mPlaces.add(myPlace);
	    		}
	    		 
	    		mName = null;
	    		mDescription = null;
	    		
    		 }
    	 }
    }
}